<template>
  <el-col class="app-v-center app-column" style="overflow-y: auto;">
    <el-row class="app-h-48px px-0 app-fill-width app-v-center px-4 app-bb-1px-l2" style="background: #dfe9f5" :gutter="10">
      <el-button
        :v-if="role < 3"
        size="mini"
        @click="toAddAward"
      >添加奖品</el-button>
    </el-row>
    <el-table
      :data="awardList"
      class="app-flex-1"
      style="width: 100%;overflow-y: auto;"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
      /> -->
      <el-table-column
        prop="name"
        align="center"
        label="奖品名称"
      />
      <el-table-column
        prop="url"
        align="center"
        label="奖品购买地址"
      />
      <el-table-column
        prop="money"
        align="center"
        label="价格"
      />
      <el-table-column
        align="center"
        width="250"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            v-if="role<3"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mb-10 mt-5"
      background
      :current-page.sync="pageNo"
      :page-sizes="[10, 20, 50]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="getAwards"
      @current-change="getAwards"
    />
    <el-dialog
      :title="isEdit? '编辑发货单' : '新增发货单'"
      :visible.sync="addAwardDialog"
      width="50vw"
      @close="cancelViewDetail"
    >
      <el-form ref="form" class="app-width-90p" :rules="rules" :model="form" label-width="160px">
        <el-form-item prop="name" label="奖品名称">
          <el-input v-model="form.name" :disabled="viewDetail" />
        </el-form-item>
        <el-form-item prop="url" label="奖品购买地址">
          <el-input v-model="form.url" :disabled="viewDetail" />
        </el-form-item>
        <el-form-item prop="money" label="奖品价格">
          <el-input v-model.number="form.money" type="number" :disabled="viewDetail" />
        </el-form-item>
        <el-form-item prop="image" label="奖品图片">
          <el-upload
            ref="uploader"
            list-type="picture-card"
            class="avatar-uploader"
            :limit="1"
            action="https://www.wangzhengqiu.com/event/prize/image"
            :show-file-list="true"
            :on-success="handleAvatarSuccess"
            :on-exceed="outOfControl"
            :on-remove="removeImage"
            :file-list="fileList"
          >
            <i class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addAwardDialog = false">取 消</el-button>
        <el-button type="primary" @click="addAward">确 定</el-button>
      </div>
    </el-dialog>
  </el-col>
</template>

<script>
import { updateAward,deleteAward,addAward,getAwards } from '@/api/award'
import { parseAdress } from '@/api/dispatch'
export default {
  data: () => ({
    // 表单验证规则
    rules: {
      name: [
        { required: true, message: '请输入奖品名称', trigger: 'blur' }
      ],
      money: [
        { required: true, message: '请输入奖品价格', trigger: 'blur' }
      ],
      url: [
        { required: true, message: '奖品购买地址是必填的', trigger: 'blur' }
      ],
      image: [
        { required: true, message: '图片是必传的', trigger: 'blur' }
      ]
    },
    isEdit: false,
    viewDetail: false,
    role: Number(JSON.parse(localStorage.getItem('userInfo')).role),
    addAwardDialog: false,
    awardList: [],
    fileList: [],
    pageNo: 1,
    pageSize: 10,
    total: 0,
    form: {
      name: '',
      money: null,
      image: null,
      url: null
    }
  }),
  mounted() {
    this.getAwards()
  },
  methods: {
    // 接受图片url
    handleAvatarSuccess(res, file) {
      if (res.code === 200) {
        this.form.image = res.data
        this.$message.success('上传成功')
      } else {
        this.$message('上传失败！')
        this.form.image = null
        this.fileList = []
      }
    },
    removeImage() {
      this.form.image = ''
    },
    outOfControl(files, fileList) {
      this.$message('只能上传一张图片')
    },
    beforeAvatarUpload(file) {
      const isLt = file.size / 1024 / 1024 < 10

      if (!isLt) {
        this.$message.error('上传头像图片大小不能超过 10MB!')
      }
      return isLt
    },
    getAwards() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize
      }
      getAwards(params).then(res => {
        if (res.code === 200) {
          this.awardList = res.data.dataList
          this.total = res.data.total
        }
      })
    },
    toAddAward() {
      this.selectAdress = []
      this.fileList = []
      this.form = {
        name: '',
        money: null,
        url: null
      }
      this.isEdit = false
      this.addAwardDialog = true
    },
    toViewDetail(index,row) {
      this.viewDetail = true
      this.form = row
      this.selectAdress = []
      const { province,city,county } = row
      if (province && city && county) {
        this.selectAdress = [province,city,county]
      }
      this.addAwardDialog = true
    },
    cancelViewDetail() {
      this.viewDetail = false
      this.addAwardDialog = false
    },
    changeAdress() {
      // 省市区未选全解析详细地址
      parseAdress({ address: this.form.address }).then(res => {
        const { province,city,county,phonenum } = res.data
        this.form = {
          ...this.form,
          province,city,county
        }
        if (province && city && county) {
          // 有结果
          this.selectAdress = [province,city,county]
        }
        if (phonenum) {
          this.form.consigneeMobile = phonenum
        }
      })
      // if (this.selectAdress.length < 3) {
      // }
    },
    handleEdit(index,row) {
      this.isEdit = true
      this.form = row
      this.selectAdress = []
      const { province,city,county } = row
      if (province && city && county) {
        this.selectAdress = [province,city,county]
      }
      if (row.image) {
        this.fileList = [{ name: '1',url: row.image }]
      } else {
        this.fileList = []
      }
      this.addAwardDialog = true
      this.changeAdress()
    },
    handleDelete(index,row) {
      this.$confirm(`确认删除货单${row.name}？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAward({ id: row.id }).then(res => {
          if (res.data.code === 200) {
            this.$message('删除成功')
            this.pageNo = 1
            this.getAwards()
          }
        })
      }).catch(() => {
        this.$message('已取消')
      })
    },
    addAward() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // if (!this.form.address) {
          //   this.$message({ type: 'warning',message: '请填写详细地址' })
          //   return
          // }
          // this.form.address = this.form.address.join('').concat(this.detailAdress)
          if (this.isEdit) {
            updateAward(this.form).then(res => {
              if (res.code === 200) {
                this.$message({ type: 'success',message: '操作成功' })
                this.addAwardDialog = false
                this.getAwards()
              }
            })
          } else {
            addAward(this.form).then(res => {
              if (res.code === 200) {
                this.$message({ type: 'success',message: '操作成功' })
                this.addAwardDialog = false
                this.pageNo = 1
                this.getAwards()
              }
            })
          }
        }
      })
    },
    // 拿到地址
    getTown(e) {
      this.address = e
    }
  }
}
</script>

<style lang="scss" scoped>
 ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
